package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Comment;
import com.itheima.mapper.CommentMapper;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo<Comment> findByPage(int pageNum, int pageSize, String creatorName, String title) {
        PageHelper.startPage(pageNum, pageSize);
        creatorName = (creatorName != null ? creatorName.trim() : creatorName);
        title = (title != null ? title.trim() : title);
        List<Comment> list = commentMapper.findByPage(creatorName, title);
        return new PageInfo<>(list);
    }

    @Override
    public Comment findById(Integer id) {
        return commentMapper.findById(id);
    }

    @Override
    public List<Comment> findByMovieId(Integer moviesId) {
        return commentMapper.findByMovieId(moviesId);
    }

    @Override
    public void save(Comment comment) {
        commentMapper.save(comment);
    }

    @Override
    public void update(Comment comment) {
        commentMapper.update(comment);
    }

    @Override
    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        if (ArrayUtil.isNotEmpty(ids)) {
            commentMapper.deleteByIds(ids);
        }
    }
}
